/*
 * @Author: your name
 * @Date: 2021-05-02 20:40:35
 * @LastEditTime: 2021-05-11 11:30:47
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \studygo\锁\读写锁\main.go
 */
package main

import (
	"fmt"
	"strconv"
	"sync"
	"time"
)

/**
读写锁
*/
var rwLock sync.RWMutex

func main() {
	for i := 0; i < 5; i++ {
		go func(i int) {
			rwLock.RLocker()
			defer rwLock.RLocker()
			fmt.Println("read func " + strconv.Itoa(i) + " get rlock at" + time.Now().String())
			time.Sleep(time.Second)
		}(i)
	}

	time.Sleep(time.Second / 10)
	for i := 0; i < 5; i++ {
		go func(i int) {
			rwLock.RLock()
			defer rwLock.RUnlock()
			fmt.Println("write func " + strconv.Itoa(i) + " get wlock at" + time.Now().String())
			time.Sleep(time.Second)
		}(i)
	}
	//保证所有的goroutime执行结束
	time.Sleep(time.Second * 10)
}
